<?php
namespace app\common\model;

use think\helper\Hash;
/**
 * 会员模型
 */
class Member extends Base 
{
	// 自动写入时间戳
    protected $autoWriteTimestamp = true;

    // 对密码进行加密
    public function setPasswordAttr($value)
    {
        return Hash::make((string)$value);
    }

    public function group(){
    	return $this->belongsTo('MemberLevel','level','id');
    }

    /**
     * 会员登录
     * @param string $username 用户名
     * @param string $password 密码
     * @return bool|mixed
     */
    public function login($username = '', $password = '')
    {
    	$username = trim($username);
        $password = trim($password);
        // 用户名登录
        $map['username'] = $username;
        $map['status'] = 1;
        //查找会员
        $user = self::where($map)->find();
        if(!$user){
        	$this->error = '会员不存在或被禁用!';
        }else{
        	if (!Hash::check((string)$password, $user['password'])) {
                $this->error = '账号或者密码错误！';
            }else{
            	$uid = $user['id'];
            	$auth = [
                	'uid'        => $uid,
                	'avatar'	 => $user['avatar'],
                	'username'   => $user['username'],
                	'nickname'   => $user['nickname']
                ];
                session('member_auth', $auth);
        		session('member_auth_sign', data_auth_sign($auth));
                return $uid;
            }
        }
    }   
}